from django.shortcuts import render

# Create your views here.
from rest_framework.generics import ListAPIView, UpdateAPIView
from apps.invite.serializer import RecommendSerializer, EnterpriseSerializer, EnterpriseDetailSerializer, \
    HotCitySerializer, RecruitSerializer
from apps.invite.models import Recruit, Enterprise, City
from rest_framework.views import APIView
from rest_framework.response import Response
from apps.invite.paginate import PageNum
from rest_framework.generics import RetrieveAPIView
import json

from apps.invite.models import Enterprise
from apps.disclose.serializers import EnterpriseListSerializer,EnterpriseVisitSerializer

from apps.users.models import User


class RecommendListAPIView(ListAPIView):
    """推荐职位"""

    queryset = Recruit.objects.all().order_by('-visits')[0:4]
    serializer_class = RecommendSerializer
    # pagination_class = PageNum


class RecruitsSearchView(ListAPIView):
    """最新职位"""

    queryset = Recruit.objects.all().order_by('-createtime')[0:4]
    serializer_class = RecommendSerializer
    # pagination_class = PageNum


class HotEnterpriseAPIView(APIView):
    """热门企业"""

    def get(self, request):
        enterprise = Enterprise.objects.all().order_by("-visits")[0:6]
        serializer = EnterpriseSerializer(enterprise, many=True)
        return Response(serializer.data)


class HotCityView(APIView):
    """热门城市"""

    def get(self, request):
        city = City.objects.filter(ishot='1')
        serializer = HotCitySerializer(city, many=True)
        return Response(serializer.data)


class HotCityKeywordView(APIView):
    """关键字搜索"""

    def post(self, request):

        # 接收参数
        dict = json.loads(request.body.decode())
        cityname = dict.get('cityname')
        keyword = dict.get('keyword')

        if cityname and keyword:

            recruit = Recruit.objects.filter(city=cityname, jobname__contains=keyword)

            serializer = RecruitSerializer(recruit, many=True)

            return Response(serializer.data)

        elif cityname:

            recruit = Recruit.objects.filter(city=cityname)

            serializer = RecruitSerializer(recruit, many=True)

            return Response(serializer.data)

        else:

            recruit = Recruit.objects.filter(jobname__contains=keyword)

            serializer = RecruitSerializer(recruit, many=True)

            return Response(serializer.data)


class RecruitsDetailView(APIView):
    """职位详情"""
    def get(self, request, id):
        recruit = Recruit.objects.get(pk=id)

        serializer = RecruitSerializer(recruit)

        return Response(serializer.data)


class RecruitsDetailVisitView(APIView):
    """职位浏览量"""
    def put(self, request, id):
        recruit = Recruit.objects.get(id=id)
        recruit.visits += 1
        recruit.save()

        return Response({'success': True,
                         "message": "更新成功"})


class RecruitsCollectView(APIView):
    """收藏职位"""
    def post(self, request, id):
        recruit = Recruit.objects.get(id=id)

        user = User.objects.get(username=request.user.username)

        recruit.users.add(user)

        recruit.save()

        return Response({
            "message": "收藏成功",
            "success": True
        })


class CancelCollectAPIView(APIView):
    """取消收藏职位"""
    def post(self, request, id):
        recruit = Recruit.objects.get(id=id)

        user = User.objects.get(username=request.user.username)

        recruit.users.remove(user)

        recruit.save()

        return Response({
            "message": "取消收藏",
            "success": True
        })


class EnterpriseListAPIView(RetrieveAPIView):
    """公司详情页面展示"""
    def get_object(self):
        id = self.kwargs.get('id')
        return Enterprise.objects.get(id=id)


    def get_serializer_class(self):
        return EnterpriseListSerializer


class EnterpriseVisitAPIView(UpdateAPIView):
    """公司页面浏览量"""
    def get_object(self):
        id = self.kwargs.get('id')
        return Enterprise.objects.get(id=id)

    def get_serializer_class(self):
        return EnterpriseVisitSerializer

    def update(self, request, *args, **kwargs):
        retrieve = self.get_object()
        retrieve.visits += 1
        retrieve.save()
        return Response({"message":"更新成功","success":True})


class EnterpriseCollectAPIView(APIView):
    """收藏公司"""

    def post(self, request, id):
        enterprise = Enterprise.objects.get(id=id)

        user = User.objects.get(username=request.user.username)

        enterprise.users.add(user)

        enterprise.save()

        return Response({
            "message": "收藏成功",
            "success": True
        })

class EnterpriseCancelCollectView(APIView):
    """取消收藏公司"""

    def post(self, request, id):
        enterprise = Enterprise.objects.get(id=id)

        user = User.objects.get(username=request.user.username)

        enterprise.users.remove(user)

        enterprise.save()

        return Response({
            "message": "取消收藏",
            "success": True
        })
